<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 菜单添加
 */
class MenuController extends CommonController {

    var $formatTree;

    /**
     * 菜单列表
     */
    public function index() {
        $pid=I("get.pid");
        $map['pid'] = $pid;
        $list = $this->lists("Menu", $map);
        $this->assign("_list", $list);
        $this->assign("pid", $pid);
        $this->meta_title = '菜单列表';
        $this->display();
    }

    /**
     * 菜单添加
     */
    public function add() {
        if (IS_POST) {
            if (D("Menu")->create()) {
                if (D("Menu")->add()) {
                    $this->success("操作成功", U('index',array('pid' => I('pid', 0))));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error(D("Menu")->getError());
            }
        }

        $this->assign('info', array('pid' => I('get.pid', 0)));
        $menus = M('Menu')->field(true)->select();

        $tree = list_to_tree($menus);

        $this->formatTree = array();
        $this->_toFormatTree($tree);

        $this->assign("tree", $this->formatTree);


        $this->meta_title = '添加菜单';
        $this->display("edit");
    }

    //生成树形
    private function _toFormatTree($list, $level = 0, $title = 'title') {
        foreach ($list as $key => $val) {
            $tmp_str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level * 2);
            $tmp_str.="└";

            $val['level'] = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . "&nbsp;" : $tmp_str . $val[$title] . "&nbsp;";
            // $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
            if (!array_key_exists('_child', $val)) {
                array_push($this->formatTree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->formatTree, $val);
                $this->_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归
            }
        }
        return;
    }

    public function icon() {
        $this->display();
    }

    /**
     * 菜单编辑
     */
    public function edit() {
        $id = I("id");
        if (IS_POST) {
            $is_hide=I("hide");
            if(empty($is_hide)){
                $_POST["hide"]=0;
            }
            $is_dev=I("is_dev");
            if(empty($is_dev)){
                $_POST["is_dev"]=0;
            }
            
            if (D("Menu")->create()) {
                if (D("Menu")->save()) {
                    $this->success("操作成功", U('index',array('pid' => I('get.pid', 0))));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error(D("Menu")->getError());
            }
        }
        
        $menus = M('Menu')->field(true)->select();

        $tree = list_to_tree($menus);

        $this->formatTree = array();
        $this->_toFormatTree($tree);

        $this->assign("tree", $this->formatTree);

        $info = D("Menu")->where(array("id" => $id))->find();
        $this->assign("info", $info);
        $this->meta_title = '编辑菜单';
        $this->display();
    }

    /**
     * 菜单删除
     */
    public function del() {
        $this->display();
    }

}
